package com.example.jf.jfapp.adapter;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.example.jf.jfapp.BlogDetailActivity;
import com.example.jf.jfapp.R;
import com.example.jf.jfapp.bean.response.BlogResponse;
import com.google.gson.Gson;

import java.util.ArrayList;
import java.util.List;

import static com.example.jf.jfapp.BlogDetailActivity.BEAN;

/**
 * Created by jf on 2017/7/27.
 */

public class XuexiAdapter extends RecyclerView.Adapter {
    private static final int TYPE_HEAD = 0;//头布局
    private static final int TYPE_FOOTER = 1;//尾布局
    private static final int TYPE_NORMAL = 2;//默认的布局

    List<String> contentArr;

    ArrayList<BlogResponse.ResultBean> mResultBean;

    private View mHeadView;
    private View mFooterView;

    public XuexiAdapter(ArrayList<BlogResponse.ResultBean> mResultBean) {
        this.mResultBean = mResultBean;
    }


    public void setHeadView(View view) {
        mHeadView = view;
    }
    /**
     * 设置尾部的View
     *
     * @param view 想要显示的View
     */
    public void setFooterView(View view) {
        mFooterView = view;
    }

    @Override
    public int getItemViewType(int position) {
        if (mHeadView != null && position == 0) {
            return TYPE_HEAD;
        }
        if (mFooterView != null && position == getItemCount() - 1) {
            return TYPE_FOOTER;
        }
        return TYPE_NORMAL;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        //判断当前是否应该设置头布局
        if (viewType == TYPE_HEAD) {
            return new HeadViewHolder(mHeadView);
        }
        if (viewType == TYPE_FOOTER) {
            return new FooterViewHolder(mFooterView);
        }
        View view= LayoutInflater.from(parent.getContext()).inflate(R.layout.item_xuexi,parent,false);
        ItemViewHolder itemViewHolder=new ItemViewHolder(view);
        return itemViewHolder;
    }

    class FooterViewHolder extends RecyclerView.ViewHolder{
        public FooterViewHolder(View itemView) {
            super(itemView);
        }
    }

    class HeadViewHolder extends RecyclerView.ViewHolder {

        public HeadViewHolder(View itemView) {
            super(itemView);
        }
    }


    class ItemViewHolder extends RecyclerView.ViewHolder {
        ImageView fenleitupian;
        TextView boketimu;
        TextView liulanrenshu;
        TextView riqi;
        ImageView zuozhe_userhead;
        TextView zuozhexingming;
        public ItemViewHolder(View itemView) {
            super(itemView);
            fenleitupian=(ImageView)itemView.findViewById(R.id.fenleitupian);
            boketimu= (TextView) itemView.findViewById(R.id.boketimu);
            liulanrenshu= (TextView) itemView.findViewById(R.id.liulanrenshu);
            riqi= (TextView) itemView.findViewById(R.id.riqi);
            zuozhe_userhead= (ImageView) itemView.findViewById(R.id.zuozhe_userhead);
            zuozhexingming= (TextView) itemView.findViewById(R.id.zuozhexingming);
        }
    }

    @Override
    public void onBindViewHolder(final RecyclerView.ViewHolder holder, int position) {

        //position   ==   0  mHeadView    HeadViewHolder

        //判断当前是否是头布局，如果是头布局，那么直接return
        if (isHead(position)) {
            return;
        }

        if (isFooter(position)) {
            return;
        }

        //这里需要注意，取值应该是从position-1 开始，因为position ==0 已经被mHeadView占用了
        if (mHeadView != null) {
            position = position - 1;
        }
        final ItemViewHolder viewHolder = (ItemViewHolder) holder;
        final BlogResponse.ResultBean bean = mResultBean.get(position);
        viewHolder.boketimu.setText(bean.getTitle());
        viewHolder.liulanrenshu.setText(bean.getLikeSize()+"");
        viewHolder.zuozhexingming.setText(bean.getAuthor().getUsername());
        String data=bean.getTimestamp()+"";
        viewHolder.riqi.setText(data);
        Glide.with(holder.itemView.getContext())
                .load(bean.getAuthor().getHeadProtrait())
//                .error(R.mipmap.ic_error)//失败的显示的图片
//                .dontAnimate()
                .centerCrop()
                .diskCacheStrategy(DiskCacheStrategy.SOURCE)// DiskCacheStrategy.NONE 缓存
                //三级缓存
                //1. 内存缓存,保存到运行内存中，ram
                //2. 保存到本地，sd卡中
                //3. 保存到网络中
                .into(((ItemViewHolder) holder).zuozhe_userhead);

        if (bean.getBlogType() == 1) {
            ((ItemViewHolder) holder).fenleitupian.setImageResource(R.drawable.ic_chrome_reader_mode_black_24dp);
        }
        if (bean.getBlogType() == 2) {
            ((ItemViewHolder) holder).fenleitupian.setImageResource(R.drawable.ic_live_help_black_24dp);
        }
        if (bean.getBlogType() == 3) {
            ((ItemViewHolder) holder).fenleitupian.setImageResource(R.drawable.ic_movie_filter_black_24dp);
        }

//        else {
//            ((ItemViewHolder) holder).fenleitupian.setImageResource(R.drawable.ic_assignment_black_24dp);
//        }
        ((ItemViewHolder) holder).itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent =new Intent(((ItemViewHolder) holder).itemView.getContext(), BlogDetailActivity.class);
                //记得要序列化！！
                intent.putExtra(BEAN, bean);
                ((ItemViewHolder) holder).itemView.getContext().startActivity(intent);
            }
        });

//        Glide.with(holder.itemView.getContext())
//                .load(bean.getAuthor().getHeadProtrait())
////                .error(R.mipmap.ic_error)//失败的显示的图片
////                .dontAnimate()
//                .centerCrop()
//                .diskCacheStrategy(DiskCacheStrategy.SOURCE)// DiskCacheStrategy.NONE 缓存
//                //三级缓存
//                //1. 内存缓存,保存到运行内存中，ram
//                //2. 保存到本地，sd卡中
//                //3. 保存到网络中
//                .into(((ItemViewHolder) holder).zuozhe_userhead);


//        String urlpc1=bean.getUser().getImageUrl();
//        Picasso.with(context).load(urlpc1).into(viewHolder.comment_userhead);
//        // viewHolder.item_userhead.getHttpBitmap(bean.getAuthor().getHeadProtrait());
//        viewHolder.comment_username.setText(bean.getUser().getUsername());
//        viewHolder.comment_content.setText(bean.getContent());
//        viewHolder.comment_creatat.setText(bean.getCreatedAt());
    }

    private boolean isHead(int position) {
        return mHeadView != null && position == 0;
    }

    private boolean isFooter(int position) {
        //getItemCount 获取item的个数
        return mFooterView != null && position == getItemCount() - 1;
    }

    @Override
    public int getItemCount() {
        return mResultBean.size() + (mHeadView != null ? 1 : 0) + (mFooterView != null ? 1 : 0);
    }
}
